const momentService = require('../services/momentService');
const inspirecloud = require('@byteinspire/api');

class momentController {

    async listAll(ctx) {
        console.log(ctx);
        const {newsLen} = ctx.request.query;
        console.log(newsLen);
        const momentTable = inspirecloud.db.table('moment');
        const list = await momentTable.where().sort().skip(parseInt(newsLen)).limit(5).find();
        ctx.body = {list};
    }

    async create(ctx) {
        console.log(ctx);
        const {images,content,address,userid = 1,favor_count = [],viewer_count = 1,comment_count = 0} = ctx.request.body;
        console.log(images);
        
        const result = await momentService.create({images, content,address, userid, favor_count,viewer_count, comment_count});
        ctx.body = {result};
    }

    async delete(ctx) {
        await momentService.delete(ctx.params.id);
        ctx.body = {ok: true};
    }

    async done(ctx) {
        const {newsID, images, content, userid = 1} = ctx.request.body;
        await momentService.update(newsID, {images, content, userid });
        ctx.body = {ok: true};
    }

    async like(ctx) {
        const {userID, newsID} = ctx.request.body;
        await momentService.addlLike(newsID, userID);
    }

    async cancelLike(ctx) {
        const {userID, newsID} = ctx.request.body;
        await momentService.cancelLike(newsID, userID);
    }

    async addComment(ctx) {
        const {userID, newsID} = ctx.request.body;
        await momentService.cancelLike(newsID, userID);
    }

    //获取动态详情
    async getNewsDetail(ctx) {
        const {newsID} = ctx.request.query;
        console.log(newsID);
        const newsDetail = await momentService.getNewsDetail(newsID);
        ctx.body = {newsDetail}
    }
}

module.exports = new momentController();